<?php
/**
 * Class for custom tags
 *
 * @author André Silva
 */
class Tag {
	/**
	 * @var Tag
	 */
	protected $parent;
	/**
	 * @var array
	 */
	protected $properties;
	/**
	 * @var string
	 */
	protected $content;

	function __construct(&$parent, $content = '', $properties = array()) {
		$this->parent = $parent;
		$this->content = $content;
		$this->properties = $properties;
	}

	/**
	 * Parse the tag content
	 */
	public function parse() {
		$this->content = '<php:'.lcfirst(get_class($this)).'/>';
	}

	/**
	 * Replace strings inside the tag context
	 * @param string $string
	 * @param string $replacement
	 */
	public function replace($string, $replacement) {
		$this->content = str_replace($string, $replacement, $this->content);
	}

	/**
	 * Get the value for a virtual path based in the current tag
	 * @param string $property
	 * @return mixed
	 */
	protected function getValue($property = null) {
		if ($property === null) {
			return $this->parent;
		}

		if(!isset($this->properties[$property])) {
			return null;
		}
		$value = $this->properties[$property];

		if ($value == 'true' || $value == 'false') {
			$value = $value == 'true' ? true : false;
		} elseif (is_numeric($value)) {
			if (is_integer($value)) {
				$value = (int)$value;
			} elseif (is_float((float)$value)) {
				$value = (float)$value;
			}
		} elseif (strpos($value, '.')) {
			$value = Parser::getValue($this->parent, $value);
		} elseif (is_string($value)) {
			$value = $this->parent->$value;
		}

		return $value;
	}

	/**
	 * Get all properties in html text format
	 * @return string
	 */
	protected function getPropertiesString() {
		$properties = '';
		foreach ($this->properties as $key => $value) {
			$properties .= " {$key}=\"{$value}\"";
		}
		return $properties;
	}

	/**
	 * Get the tag properties
	 * @return array
	 */
	public function getProperties() {
		return $this->properties;
	}

	/**
	 * Set the tag properties
	 * @param array $properties
	 */
	public function setProperties($properties) {
		$this->properties = $properties;
	}

	/**
	 * Set the teg content
	 * @return string
	 */
	public function getContent() {
		return $this->content;
	}

	/**
	 * Set the tag content
	 * @param string $content
	 */
	public function setContent($content) {
		$this->content = $content;
	}
}
?>
